﻿package ua.mihaylenko.cardsplugin.colorpicker{
	/**
	Static color utility class which helps converting color values 
	from one format  to another
	
	@author Sharwan Kami <sharwan@gmail.com>
	@version 1.0
	@history 2009-05-10
	
	* 
	* This file is part of E-Card plugin, from zetaprints.com
	* 
	
	@usage
	Activate with:
		<code>
			//use class_name(.)method_name to call the function
			
			ZColor.hexToRgb("0xff0000");
			//returns array object {r:1,g:0,b:0}
			
			//similarly other methods can be called.
		</code>
	
	*/
	public class ZColor{
		protected static const RGBMAX:uint = 255;//maximum RGB value
		protected static const HLSMAX:uint = 240;//maximum Hue/Luminance/Saturation value
		protected static var pre_default_ = { r: 0, g: 0, b: 0 };//default color to return
		
		public static function formatHex(hex:String):String {
    		if (hex.substr(0, 1) == '0x') {
        		return hex.substr(1);
   			} else {
      		  return hex;
    		}
		}
		
		public static function rgbToHex(r, g, b, includeHash):String {
			r = Math.round(r * 255);
			g = Math.round(g * 255);
			b = Math.round(b * 255);
			if (includeHash == undefined) {
				includeHash = true;
			}
		
			r = r.toString(16);
			if (r.length == 1) {
				r = '0' + r;
			}
			g = g.toString(16);
			if (g.length == 1) {
				g = '0' + g;
			}
			b = b.toString(16);
			if (b.length == 1) {
				b = '0' + b;
			}
			return ((includeHash ? '0x' : '') + r + g + b).toUpperCase();
		}
		
		public static function hexToRgb(hex_string) {
			if (hex_string.substr(0, 2) == '0x') {
				hex_string = hex_string.substr(2);
			}
		
			var r;
			var g;
			var b;
			trace(hex_string);
			if (hex_string.length == 3) {
				r = hex_string.substr(0, 1);
				r += r;
				g = hex_string.substr(1, 1);
				g += g;
				b = hex_string.substr(2, 1);
				b += b;
			}else if (hex_string.length == 6) {
				r = hex_string.substr(0, 2);
				g = hex_string.substr(2, 2);
				b = hex_string.substr(4, 2);
			}else {
				return pre_default_;
			}
		
			r = parseInt(r, 16);
			g = parseInt(g, 16);
			b = parseInt(b, 16);

			if (isNaN(r) || isNaN(g) || isNaN(b)) {
				return pre_default_;
			}
			else {
				pre_default_ = { r: r / 255, g: g / 255, b: b / 255 };
				return { r: r / 255, g: g / 255, b: b / 255 };
			}
		}
		
		public static function rgbToHsv(red, green, blue) {
			var max = Math.max(Math.max(red, green), blue);
			var min = Math.min(Math.min(red, green), blue);
			var hue;
			var saturation;
			var value = max;
			if (min == max) {
				hue = 0;
				saturation = 0;
			}
			else {
				var delta = (max - min);
				saturation = delta / max;
				if (red == max) {
					hue = (green - blue) / delta;
				}
				else if (green == max) {
					hue = 2 + ((blue - red) / delta);
				}
				else {
					hue = 4 + ((red - green) / delta);
				}
				hue /= 6;
				if (hue < 0) {
					hue += 1;
				}
				if (hue > 1) {
					hue -= 1;
				}
			}
			return {
				h: hue,
				s: saturation,
				v: value
			};
		}
		
		public static function hsvToRgb(hue, saturation, value) {
			var red;
			var green;
			var blue;
			if (value == 0.0) {
				red = 0;
				green = 0;
				blue = 0;
			}
			else {
				var i = Math.floor(hue * 6);
				var f = (hue * 6) - i;
				var p = value * (1 - saturation);
				var q = value * (1 - (saturation * f));
				var t = value * (1 - (saturation * (1 - f)));
				switch (i) {
					case 1: red = q; green = value; blue = p; break;
					case 2: red = p; green = value; blue = t; break;
					case 3: red = p; green = q; blue = value; break;
					case 4: red = t; green = p; blue = value; break;
					case 5: red = value; green = p; blue = q; break;
					case 6: // fall through
					case 0: red = value; green = t; blue = p; break;
				}
			}
			return { r: red, g: green, b: blue };
		}		
		
		public static function getRGB(color):String{
			var colorInHex:String = color.toString(16);
			var c:String = "00000" + colorInHex;
			var e:int = c.length;
			c = c.substring(e - 6, e);
			return c.toUpperCase();			
		}
		
		public static function colorValidator(hexString:String){
			var r,g,b;
			
			if(hexString.length==3){
				r = hexString.substr(0, 1);
        		r += r;
        		g = hexString.substr(1, 1);
       			g += g;
        		b = hexString.substr(2, 1);
        		b += b;
			}else if(hexString.length==6){
				r=hexString.substr(0, 2);
        		g=hexString.substr(2, 2);
       			b=hexString.substr(4, 2);
			}else{
				return false;
			}
			
			r=parseInt(r,16);
			g=parseInt(g,16);
			b=parseInt(b,16);
			
			if(isNaN(r) || isNaN(g) || isNaN(b)){
				return false;
			}else{
				return true;
			}
		}
	}
}